﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Imaging;

namespace Galaktika.BI.Silverlight.ClientApp.Items
{
    public class HorizontalExpander:Grid
    {
        private TextBlock m_Caption;
        private Button m_ExpandButton;
        //private ContentControl m_Content;

        public HorizontalExpander()
        {
            base.RowDefinitions.Add(new RowDefinition() { Height = GridLength.Auto });
            base.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(0.0, GridUnitType.Pixel) });
            base.ColumnDefinitions.Add(new ColumnDefinition() { Width  = new GridLength(1.0, GridUnitType.Star) });

            Grid header = new Grid();
            header.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Auto });
            header.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(1.0, GridUnitType.Star) });
            header.HorizontalAlignment = HorizontalAlignment.Stretch;
   
            base.Children.Add(header);

            m_ExpandButton = new Button();
            m_ExpandButton.Style = App.Current.Resources["ButtonForm"] as Style; 
            Image img = new Image();
            img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Down.png", UriKind.Relative));
            m_ExpandButton.Content = img;
            m_ExpandButton.Click += new RoutedEventHandler(ExpandButton_Click);
            m_ExpandButton.Width = 20;
            m_ExpandButton.Height = 20;
            m_ExpandButton.HorizontalAlignment = HorizontalAlignment.Right;   
            Grid.SetColumn(m_ExpandButton, 1); 
            header.Children.Add(m_ExpandButton);

            m_Caption = new TextBlock();
            m_Caption.HorizontalAlignment = HorizontalAlignment.Stretch; 
            m_Caption.Margin = new Thickness(2, 2, 2, 2);  
            Grid.SetColumn(m_Caption, 0);
            header.Children.Add(m_Caption);

            //m_Content = new ContentControl();
            //m_Content.HorizontalScrollBarVisibility =
            //    m_Content.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        }

        void ExpandButton_Click(object sender, RoutedEventArgs e)
        {
            this.IsExpanded = !this.IsExpanded;
        }

        private void UpdateView()
        {
            if (this.Content != null)
            {
                if (m_IsExpanded)
                {
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Up.png", UriKind.Relative));
                    m_ExpandButton.Content = img;
                    base.RowDefinitions[1].Height = new GridLength(1.0, GridUnitType.Star);
                    this.Content.Visibility = Visibility.Visible;
                    this.RestoreHeight();
                }
                else
                {
                    Image img = new Image();
                    img.Source = new BitmapImage(new Uri("/Galaktika.BI.Silverlight.ClientApp;component/Items/Images/Down.png", UriKind.Relative));
                    m_ExpandButton.Content = img;
                    base.RowDefinitions[1].Height = new GridLength(0.0);
                    this.Content.Visibility = Visibility.Collapsed;
                    this.SaveHeight();
                }
            }
        }

        class GridLengthHolder
        {
            public GridLengthHolder(GridLength length)
            {
                this.Length = length;
            }

            public GridLength Length;
        }

        private GridLengthHolder m_Height;
        private void SaveHeight()
        {
            if (this.Parent == null) return;
            Grid parent = this.Parent as Grid;
            if (parent == null)
            {
                FrameworkElement border = this.Parent as FrameworkElement;
                if (border != null)
                {
                    parent = border.Parent as Grid;
                }
            }
            if (parent != null)
            {
                int rowIndex = Grid.GetRow(this);
                if (rowIndex < parent.RowDefinitions.Count)
                {
                    m_Height = new GridLengthHolder(parent.RowDefinitions[rowIndex].Height);
                    parent.RowDefinitions[rowIndex].Height = GridLength.Auto;
                }
            }
        }

        private void RestoreHeight()
        {
            if (this.Parent == null) return;
            Grid parent = this.Parent as Grid;
            if (parent == null)
            {
                FrameworkElement border = this.Parent as FrameworkElement;
                if (border != null)
                {
                    parent = border.Parent as Grid;
                }
            }
            if (parent != null)
            {
                int rowIndex = Grid.GetRow(this);
                if (rowIndex < parent.RowDefinitions.Count)
                {
                    if (m_Height != null)
                    {
                        parent.RowDefinitions[rowIndex].Height = m_Height.Length;
                    }
                    else
                    {
                        // parent.RowDefinitions[rowIndex].Height = значение по умолчанию
                    }
                }
            }
        }

        private bool m_IsExpanded = false;
        public bool IsExpanded
        {
            get
            {
                return m_IsExpanded;
            }
            set
            {
                m_IsExpanded = value;
                this.UpdateView();
            }
        }


        public string Caption
        {
            get
            {
                return m_Caption.Text;
            }
            set
            {
                m_Caption.Text = value;
            }
        }

        private FrameworkElement m_Content;
        public FrameworkElement Content 
        {
            get
            {
                return m_Content;
            }
            set
            {
                if (m_Content != null)
                {
                    base.Children.Remove(m_Content);
                }
                m_Content = value;
                m_Content.HorizontalAlignment = HorizontalAlignment.Stretch;
                m_Content.VerticalAlignment = VerticalAlignment.Stretch;
                m_Content.Visibility = Visibility.Collapsed;
                m_Content.MinHeight = 40;
                Grid.SetRow(m_Content, 1);
                base.Children.Add(m_Content);
            }
        }

        //public object Content
        //{
        //    get
        //    {
        //        return m_Content.Content;
        //    }
        //    set
        //    {
        //        m_Content.Content = value;
        //    }
        //}
    }
}
